package gae.google.tcserver.api;

import gae.google.tcserver.db.Activity;
import gae.google.tcserver.db.GetFromDB;
import gae.google.tcserver.db.Team;
import gae.google.tcserver.db.TeamMember;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//import java.util.logging.Logger;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

/**
 * The <tt>CreateJsonTeamServlet</tt> class creates a new Json including
 * information about the team. 
 *
 * @author Lars Friberg
 *
 * @date 2010-12-18
 */
public class CreateJson {

	/**
	 * constructor
	 */
	public CreateJson() {
	}
	
	/**
	 * get all teams 
	 * 
	 * @return json the teams
	 */
	public String getTeams() {

		// list needed to create the new Json
		List<Team> teams = new ArrayList<Team>();
	
		// old and new Json
		String json = "{}";
		String newJson = null;
	
	
		// create a new Jason is created including all teams
		
		/***************************************************************
		 *{
		 *	teams: {
		 *		noofteams: "x",
		 *			team1:{
		 *				id: 	"team id",
		 *				name: 	"team name",
		 *				image: 	"image name"
		 * 			},
		 * 			team2:
		 * 			......
		 * 			team3
		 * 			......	
		 * 		}
		 * }
		 **************************************************************/
		
		// get all teams from database
		teams = GetFromDB.getAllTeams();
	
		// holds the current team number 
		int teamNumber = 1;
	
		// is there any teams?
		if (teams.size() != 0) {
	
			// create Json
			try {
				JSONObject jsonObject = new JSONObject(json);
				JSONObject teamObject = new JSONObject();
			
				teamObject.put("noofteams", teams.size()); 
				for (int i = 0; i < teams.size(); i++) {
					JSONObject newTeam = new JSONObject();
					newTeam.put("id", KeyFactory.keyToString(teams.get(i).getKeyId()));
					newTeam.put("name", teams.get(i).getName());
					newTeam.put("image", teams.get(i).getImage());
					teamObject.put("team" + String.valueOf(teamNumber), newTeam);
					teamNumber++;
				}
				teams.clear();
				jsonObject.put("teams", teamObject);
				newJson = jsonObject.toString();
	
			}catch(JSONException e) {
				newJson = null;
			}
		}
		return newJson;
	}
	
	/**
	 * get all team members 
	 * 
	 * @return json the team members
	 */
	public String getTeamMembers(Key teamKeyId) {

		// list needed to create the new Json
		List<Key> teamMemberKeyIds = new ArrayList<Key>();
	
		// get current team
		Team team = GetFromDB.getTeam(teamKeyId);
		
		// get a list of member key id's 
		teamMemberKeyIds = team.getMemberKeyIds();	
		
		// old and new Json
		String json = "{}";
		String newJson = null;
	
	
		// create a new Jason is created including all team members
		
		/***************************************************************
		 *{
		 *	teammembers: {
		 *		noofteammembers: "x",
		 *					teammember1:{
		 *						id: 			"team member id",
		 *						firstname: 		"first name",
		 *						familyname: 	"family name",
		 *						address: 		"address",
		 *						zipcode: 		"zip code",
		 *						location: 		"location",
		 *						phonenumber:	"phone number",
		 *						mailaddress:"	"mail address",
		 *						role:			"role"
		 *						image: 			"image name"
		 *						teamid:			"team id"
		 * 					},
		 * 					teammember2:
		 * 					......
		 * 					teammember3
		 * 					......	
		 * 		}
		 * }
		 **************************************************************/
		
		// holds the current team member number 
		int teamMemberNumber = 1;
	
		// holds the current member
		TeamMember teamMember = null;
		
		// is there any team members?
		if (teamMemberKeyIds.size() != 0) {
	
			// create Json
			try {
				JSONObject jsonObject = new JSONObject(json);
				JSONObject teamMemberObject = new JSONObject();
			
				teamMemberObject.put("noofteammembers", teamMemberKeyIds.size()); 
				for (int i = 0; i < teamMemberKeyIds.size(); i++) {
					teamMember = GetFromDB.getTeamMember(teamMemberKeyIds.get(i));
					JSONObject newTeamMember = new JSONObject();
					newTeamMember.put("id", KeyFactory.keyToString(teamMember.getKeyId()));
					newTeamMember.put("firstname", teamMember.getFirstName());
					newTeamMember.put("familyname", teamMember.getFamilyName());
					newTeamMember.put("address", teamMember.getAddress());
					newTeamMember.put("zipcode", teamMember.getZipCode());
					newTeamMember.put("location", teamMember.getLocation());
					newTeamMember.put("phonenumber", teamMember.getPhoneNumber());
					newTeamMember.put("mailaddress", teamMember.getMailAddress());
					newTeamMember.put("role", teamMember.getRole());
					newTeamMember.put("imagename", teamMember.getImageName());
					newTeamMember.put("teamid", teamMember.getTeamKeyId());
					teamMemberObject.put("teammember" + String.valueOf(teamMemberNumber), newTeamMember);
					teamMemberNumber++;
				}
				teamMemberKeyIds.clear();
				jsonObject.put("teammembers", teamMemberObject);
				newJson = jsonObject.toString();
	
			}catch(JSONException e) {
				newJson = null;
			}
		}
		return newJson;
	}
	
	/**
	 * get all activities between dates 
	 * 
	 * @return json the activities
	 */
	public String getActivities(Date fromDate, Date toDate) {

		//final Logger log = Logger.getLogger(CreateJson.class.getName());
		// list needed to create the new Json
		List<Activity> activities = new ArrayList<Activity>();
	
		// old and new Json
		String json = "{}";
		String newJson = null;
	
	
		// create a new Jason is created including all activities
		
		/***************************************************************
		 *{
		 *	activities: {
		 *		noofactivities: "x",
		 *			activity1:{
		 *				id: 		"activity id",
		 *				date: 		"date",
		 *				activity: 	"activity"
		 * 			},
		 * 			activity2:
		 * 			......
		 * 			activity3
		 * 			......	
		 * 		}
		 * }
		 **************************************************************/
		
		// get all activities between two dates
		activities = GetFromDB.getActivities(fromDate, toDate);
	
		// holds the current activity number 
		int activityNumber = 1;
	
		// is there any activities?
		if (activities.size() != 0) {

			// create Json
			try {
				JSONObject jsonObject = new JSONObject(json);
				JSONObject activityObject = new JSONObject();
			
				activityObject.put("noofactivities", activities.size()); 
				for (int i = 0; i < activities.size(); i++) {
					JSONObject newActivity = new JSONObject();
					newActivity.put("id", KeyFactory.keyToString(activities.get(i).getKeyId()));
					newActivity.put("date", activities.get(i).getDate().getTime());
					newActivity.put("activity", activities.get(i).getActivity());
					activityObject.put("activity" + String.valueOf(activityNumber), newActivity);
					activityNumber++;
				}
				jsonObject.put("activities", activityObject);
				newJson = jsonObject.toString();
	
			}catch(JSONException e) {
				newJson = null;
			}
		}
		return newJson;
	}
}
